"""
往hero表中添加数据
"""
from pymysql import Connection

conn = None
cursor = None
try:
    conn = Connection(
        host="localhost",
        port=3306,
        user = "root",
        password = "root",
        database="db03",
        charset="utf8"
    )
    cursor = conn.cursor()
    # sql = "INSERT INTO hero VALUES(null,%s,%s,%s,%s)"
    # li = ["孙悟空",300,"男","傲来国花果山"]
    # li = ("抓八戒",150,"男","福临山云栈洞")
    # cursor.execute(sql, li)
    sql = "INSERT INTO hero VALUES(null,%(name)s,%(age)s,%(gender)s,%(address)s)"
    dict = {
        "name":"沙僧",
        "age":800,
        "gender":"男",
        "address":"流沙河"
    }
    cursor.execute(sql,dict)

    conn.commit()
    print("数据添加完成")
except Exception as e:
    print("异常",e)
finally:
    if cursor:
        cursor.close()
    if conn:
        conn.close()